using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using GF.Debug;
using GF.ResourcesLoad;

public class UIView : MonoBehaviour
{
    public List<Texture2D> sprites = new List<Texture2D>();
    public Transform Find(string path)
    {
        return transform.Find(path);
    }
    public Transform Find(string path, Transform parent)
    {
        return parent.Find(path);
    }
    public Button FindButton(string path)
    {
        return transform.Find(path).GetComponent<Button>();
    }
    public Button FindButton(string path, Transform parent)
    {
        return parent.Find(path).GetComponent<Button>();
    }
    public Text FindText(string path)
    {
        return transform.Find(path).GetComponent<Text>();
    }
    public Text FindText(string path, Transform parent)
    {
        return parent.Find(path).GetComponent<Text>();
    }
    public Image FindImage(string path)
    {
        return transform.Find(path).GetComponent<Image>();
    }
    public Image FindImage(string path, Transform parent)
    {
        return parent.Find(path).GetComponent<Image>();
    }
    public Toggle FindToggle(string path)
    {
        return transform.Find(path).GetComponent<Toggle>();
    }
    public Toggle FindToggle(string path, Transform parent)
    {
        return parent.Find(path).GetComponent<Toggle>();
    }
    public MeshRenderer Find3DImage(string path)
    {
        return transform.Find(path).GetComponent<MeshRenderer>();
    }
    public MeshRenderer Find3DImage(string path, Transform parent)
    {
        return parent.Find(path).GetComponent<MeshRenderer>();
    }
    public TextMesh Find3DText(string path)
    {
        return transform.Find(path).GetComponent<TextMesh>();
    }
    public TextMesh Find3DText(string path, Transform parent)
    {
        return parent.Find(path).GetComponent<TextMesh>();
    }
    public void CloseView()
    {
        UIMgr.CloseView();
    }
    public void ClosePop()
    {
        Destroy(gameObject);
    }
    public void Log(object o)
    {
        Debugger.Log(o);
    }
    public void DelayAction(float time, Action action)
    {
        StartCoroutine(TimeManager.DelayAction(time, action));
    }

    public void EndFrameAction(Action action)
    {
        StartCoroutine(TimeManager.EndFrameAction(action));
    }

    public void Set3DImage(MeshRenderer meshRenderer, string path)
    {
#if LCS
        meshRenderer.materials[0].mainTexture = AppFacade.Instance.ResMgr.LoadTexture(path);
#else
        meshRenderer.materials[0].mainTexture = CResourceManager.Load<Texture>(path);
#endif
    }
    public static void ChangeXYZ(Transform t, float x, float y, float z)
    {
        t.localPosition = new Vector3(x, y, z);
    }
    public static void ChangeZ(Transform t, float z)
    {
        t.localPosition = new Vector3(t.localPosition.x, t.localPosition.y, z);
    }
    public static void ChangeX(Transform t, float x)
    {
        t.localPosition = new Vector3(x, t.localPosition.y, t.localPosition.z);
    }
    public static void ChangeY(Transform t, float y)
    {
        t.localPosition = new Vector3(t.localPosition.x, y, t.localPosition.z);
    }
    public void SetImage(Image image, string path) {
        Sprite sprite = LoadSprite(path);
        image.sprite = sprite;
    }
    private void OnDestroy()
    {
       
    }
    //加载图片
    public Sprite LoadSprite(string path)
    {
#if LCS
        Texture2D t = AppFacade.Instance.ResMgr.LoadTexture(path) as Texture2D;
#else
        Texture2D t = Resources.Load<Texture2D>(path);
#endif
        if (t)
        {
            sprites.Add(t);
            return Sprite.Create(t, new Rect(new Vector2(0, 0), new Vector2(t.width, t.height)), new Vector2(0, 0));
        }
        else
        {
            Debugger.LogError("图片不存在" + path);
            //return new Sprite();
            return null;
        }
    }
}
